import os
from time import sleep

import allure
import pytest

from page.page_conversation_right_btn import PageConversation
from tool.get_driver import GetDriver
from tool.get_log import GetLogger
from tool.read_yaml import read_yaml

log = GetLogger.get_log()
module_name = str(os.path.basename(__file__)).split('.')[0]


@allure.epic("群内功能")
@allure.feature("右侧开关和消息测试")
class TestConversation():

    @classmethod
    def setup_class(cls):
        cls.driver = GetDriver.get_app_driver()
        cls.con = PageConversation(cls.driver)
        cls.con.page_init()
        cls.con.page_conversation_entrance()

    @classmethod
    def teardown_class(cls):
        # input("结束")
        GetDriver.quit_app_driver()

    @allure.title("盲盒广告位测试")
    def test01_conversation_banner(self):
        with allure.step('1.点击顶部抽盲盒广告位'):
            pass
        self.con.page_conversation_click_ad()
        try:
            with allure.step('2.断言：判断抽盲盒页面是否成功拉起'):
                pass
            print(self.con.page_conversation_ad_assert())
            print(self.driver.title)
            assert self.con.page_conversation_ad_assert()
            log.info('断言成功')
            self.con.base_get_screenshot_as_file(module_name, '断言成功截图')
            self.driver.press_keycode(4)
        except AssertionError:
            self.con.base_get_screenshot_as_file(module_name, '抽盲盒页面未成功拉起')
            log.error('断言失败')
            raise
        finally:
            self.con.page_conversation_back_url()

    @allure.title("排行榜按钮测试")
    def test02_conversation_rank(self):
        with allure.step('1.点击排行榜按钮'):
            pass
        self.con.page_conversation_click_rank()
        try:
            with allure.step('2.断言：是否成功跳转到排行榜'):
                pass
            assert self.con.page_conversation_rank_assert()
            log.info('断言成功')
            self.con.base_get_screenshot_as_file(module_name, '断言成功截图')
            self.driver.press_keycode(4)
        except AssertionError:
            self.con.base_get_screenshot_as_file(module_name, '未成功跳转')
            log.error('断言失败')
            raise
        finally:
            self.con.page_conversation_back_url()

    @allure.title("vip商城按钮测试")
    def test03_conversation_vip(self):
        with allure.step('1.点击vip按钮'):
            pass
        self.con.page_conversation_click_vip_store()
        try:
            with allure.step('2.断言：是否成功跳转到vip商城'):
                pass
            assert self.con.page_conversation_vip_store_assert()
            log.info('断言成功')
            self.con.base_get_screenshot_as_file(module_name, '断言成功截图')
            self.driver.press_keycode(4)
        except AssertionError:
            self.con.base_get_screenshot_as_file(module_name, '未成功跳转')
            log.error('断言失败')
            raise
        finally:
            self.con.page_conversation_back_url()

    @allure.title("群内成员按钮测试")
    def test04_conversation_group_people(self):
        with allure.step('1.点击群内成员按钮'):
            pass
        self.con.page_conversation_click_group_member()
        try:
            with allure.step('2.断言：是否成功跳转到成员列表'):
                pass
            assert self.con.page_conversation_group_assert()
            log.info('断言成功')
            self.con.base_get_screenshot_as_file(module_name, '断言成功截图')
            self.driver.press_keycode(4)
        except AssertionError:
            self.con.base_get_screenshot_as_file(module_name, '未成功跳转')
            log.error('断言失败')
            raise
        finally:
            self.con.page_conversation_back_url()

    @allure.title("游戏按钮测试")
    def test05_conversation_game(self):
        with allure.step('1.点击群内游戏按钮'):
            pass
        self.con.page_conversation_click_game()
        try:
            with allure.step('2.断言：是否成功跳转到游戏列表'):
                pass
            assert self.con.page_conversation_game_assert()
            log.info('断言成功')
            self.con.base_get_screenshot_as_file(module_name, '断言成功截图')
            self.driver.press_keycode(4)
        except AssertionError:
            self.con.base_get_screenshot_as_file(module_name, '未成功跳转')
            log.error('断言失败')
            raise
        finally:
            self.con.page_conversation_back_url()

    @allure.title("红包按钮测试")
    def test06_conversation_hongbao(self):
        with allure.step('1.点击群内红包按钮'):
            pass
        self.con.page_conversation_click_hongbao()
        try:
            with allure.step('2.断言：是否成功跳转到红包页面'):
                pass
            assert self.con.page_conversation_hongbao_assert()
            log.info('断言成功')
            self.con.base_get_screenshot_as_file(module_name, '断言成功截图')
            self.driver.press_keycode(4)
        except AssertionError:
            self.con.base_get_screenshot_as_file(module_name, '未成功跳转')
            log.error('断言失败')
            raise
        finally:
            self.con.page_conversation_back_url()

    @allure.title("vip入场按钮测试")
    def test07_conversation_vip_coming(self):
        with allure.step('1.点击vip入场按钮'):
            pass
        self.con.page_conversation_click_vip_coming()
        try:
            with allure.step('2.断言：是否弹出vip弹窗'):
                pass
            assert self.con.page_conversation_vip_coming_assert()
            log.info('断言成功')
            self.con.base_get_screenshot_as_file(module_name, '断言成功截图')
            self.con.page_conversation_close_vip_coming_tanchuang()
        except AssertionError:
            self.con.base_get_screenshot_as_file(module_name, '未成功跳转')
            log.error('断言失败')
            raise

    @allure.title("消息验证测试，表情需人工复验")
    @pytest.mark.parametrize("title,msg", read_yaml("chat_message.yaml"))
    def test08_conversation_message(self, title, msg):
        with allure.step(f'1.发送{title}消息{msg}'):
            pass
        self.con.page_conversation_send_message(msg)
        try:
            with allure.step(f'2.断言：{title}消息是否正确'):
                pass
            sleep(5)
            if title == "表情":
                assert True
            else:
                assert self.con.page_coversation_get_send_message()==msg
            log.info('断言成功')
            self.con.base_get_screenshot_as_file(module_name, '断言成功截图')
        except AssertionError:
            self.con.base_get_screenshot_as_file(module_name, '发送消息不相符')
            log.error('断言失败')
            raise
